<?php 
namespace app\shop\controller;
use think\Controller;

class Test extends Controller
{
	public function index () {
		$times = date('Y-m-d'); 
		$candy = db('candy_machine')->where('start','<=',$times)->where('end','>=',$times)->select();
		if ($candy) {
			foreach($candy as $v){
				if($v['phased']){
					$key = ((strtotime(date('Y-m-d'))-strtotime($v['start']))/(3600*24))/$v['phased'];
					$arr = explode(',',$v['different']);
					if(($key!=0) && is_int($key)){
						$info = [];
						$info['candy_now'] = $v['candy_now'] + ($arr[$key-1]/100*$v['candy_total']*$v['counts']);
						$info['left_days'] = (strtotime($v['end'])-strtotime(date('Y-m-d')))/(24*3600);
						$info['update_time'] = date('Y-m-d H:i:s');
						// 释放完成
						if($info['left_days'] == 0){
							$info['status'] = 1;
							$user = [];
							$users = db('ns_user')->where('id',$v['uid'])->find();
							$user['settlemen_funds'] = $users['settlemen_funds']- $v['cost'];
							$user['available_funds'] = $users['available_funds']+$v['cost']+$info['candy_now'];
							$user['total_funds'] = $users['total_funds']+$info['candy_now'];
							db("ns_user")->where('id',$v['uid'])->update($user);
						}
						db('candy_machine')->where('cm_id',$v['cm_id'])->update($info);
					}
				}
			}
		}
	}
}